package com.fsbm.ctech.maxis.ebpsbackend;

//import com.fsbm.ctech.controller.*;
import com.checkfree.isolutions.admin.ObjectId;

import java.awt.Color;
import java.io.*;
import java.util.*;
import java.text.*;

import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPageEvent;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;

import com.checkfree.isolutions.persist.PersistServer;
import com.checkfree.isolutions.statement.*;
import com.checkfree.isolutions.statement.persist.*;
import com.checkfree.isolutions.streaming.IClassId;



public class eSmsPdfDownload extends PdfPageEventHelper{

	/**
	 * Constructor
	 */
    public eSmsPdfDownload() {

        try {


        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String strStmtId="";

        for ( int i = 0; i < args.length; i++ ) {
            if (args[i].toLowerCase().indexOf("-stmtid") !=-1)
            {
                String buffer = args[i];
                int pos = buffer.indexOf("=");
                strStmtId = buffer.substring(pos+1);
                continue;
            }
        }

        // Execute program
        //PDFServlet_v3r00 pdf = new PDFServlet_v3r00();
        //pdf.StatementGeneration(strStmtId);

    }

    public void StatementGeneration(String strStmtId,String pdfFileName) {
        try {

        	//PersistServer.activate();
        	//PersistServer.beginTransaction();

            StatementHome home = StatementHomeImpl.singleton();
            int statementId = Integer.parseInt(strStmtId);
            ObjectId objectId = new ObjectId(IClassId.STATEMENT, statementId);
            Statement cfiStatement   = home.findByObjectId(objectId);

	        byte[] buffer = new byte[262144];
	        int size = 0;
	        InputStream instr   = null;
	        FileOutputStream outstr = null;
	        instr = cfiStatement.getExternalFormatData();

			outstr = new FileOutputStream(pdfFileName);
			while ((size  = instr.read(buffer, 0, buffer.length)) != -1) {
				outstr.write(buffer, 0, size);
			}
			instr.close();
			outstr.flush();
			outstr.close();


            }  catch (Exception ex) {
                System.err.println(ex.getMessage());
                ex.printStackTrace();
            }
        }


}
